import Vue from 'vue'
import VueRouter from 'vue-router'
import {Notification, MessageBox, Message} from 'element-ui'

const Login = () => import("../components/Login")
const Home = () => import('@/components/Home')
const Welcome = () => import('@/components/Welcome')
import Users from "@/components/user/Users";

const Rights = () => import('@/components/power/Rights')
const Roles = () => import('@/components/power/Roles')
const Category = () => import('@/components/goods/Category')
const Params = () => import('@/components/goods/Params')
const Goods = () => import('@/components/goods/Goods')
const Add = () => import('@/components/goods/Add')
import NotFound from "../components/common/NotFound";

Vue.use(VueRouter)

const routes = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        component: Login,
        meta: {
            title: '登录'
        }
    },
    {
        path: '/home',
        component: Home,
        meta: {
            title: "首页"
        },
        children: [
            {
                path: '/home',
                redirect: '/welcome'
            },
            {
                path: '/welcome',
                name: 'wel',
                component: Welcome,
                meta: {
                    title: "首页"
                },
            },
            {
                path: '/users',
                component: Users,
                meta: {
                    title: "首页-用户"
                }
            },
            {
                path: '/rights',
                component: Rights,
                meta: {
                    title: "首页-权限列表"
                }
            },
            {
                path: '/roles',
                component: Roles,
                meta: {
                    title: "首页-角色列表"
                }
            },
            {
                path: '/categories',
                component: Category,
                meta: {
                    title: "首页-商品分类"
                }
            },
            {
                path: '/params',
                component: Params,
                meta: {
                    title: "首页-商品参数"
                }
            },
            {
                path: '/goods',
                component: Goods,
                meta: {
                    title: "首页-商品列表"
                }
            },
            {
                path: '/goods/add',
                component: Add,
                meta: {
                    title: "首页-添加商品"
                }
            },
            {
                path: '/404',
                component: NotFound,
                meta: {
                    title: "404"
                }
            },
            {
                path: '/*',
                redirect: '/404'
            },
        ]
    }
]

const router = new VueRouter({
    routes,
})
//取消路由重复点击报错
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}
router.beforeEach(((to, from, next) => {
    // to 表示将要访问的路径
    // from 表示从哪个路径跳转过来
    // next 是一个函数，表示放行
    // next() 表示直接放行  next('/xxx')表示强制跳转

    // document.title = to.matched[0].meta.title
    if (to.path == '/login' || to.path == '/404') {
        return next()
    }
    const token = sessionStorage.getItem("token")
    if (!token) {
        Message.error("请先登录")
        next("/login")
    } else {
        return next()
    }
}))

export default router
